package com.haipumi.actions;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.ServletActionContext;
import com.haipumi.beans.JsonModel;
import com.haipumi.beans.haipumi_reply;
import com.haipumi.beans.haipumi_user;
import com.haipumi.biz.TaskBiz;
import com.haipumi.utils.CommonData;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public class TaskReplyAction extends ActionSupport implements ModelDriven<haipumi_reply>{

	private static final long serialVersionUID = -7271797784728119741L;

	private JsonModel jm;
	private HttpServletRequest request=ServletActionContext.getRequest();
	private haipumi_reply reply=new haipumi_reply();
	private String op;
	private TaskBiz taskBiz;
	
	public void setOp(String op) {
		this.op = op;
	}
	
	@Override
	public String execute() throws Exception {
		if(op!=null){
			if("addTaskReply".equals(op)){
				addTaskReplyOp( );
			}
		}
		return "success";
	}

	//添加任务评论
	private void addTaskReplyOp() {
		//没有登陆的情况下不允许添加评论
		haipumi_user user=(haipumi_user) request.getSession().getAttribute(CommonData.USERSESSION);
		if( judgeIsLogin(user) ){
			return;
		}
		String rid=taskBiz.addReply(reply,user);
		reply.setRid(rid );
		jm=new JsonModel();
		if(rid!="-1"){
			jm.setCode(1);
			jm.setObj(reply);
		}else{
			jm.setCode(0);
			jm.setMsg("添加评论失败");
		}
	}

	//判断是否账号登陆
	private boolean judgeIsLogin(haipumi_user user){
		boolean flag=false;//默认未登录
		if(user==null){
			flag=true;
			jm=new JsonModel();
			jm.setCode(0);
			jm.setMsg("请先登录...");
		}
		return flag;
	}

	@Override
	public haipumi_reply getModel() {
		return reply;
	}

	@Resource(name = "taskBizImpl")
	public void setTaskBiz(TaskBiz taskBiz) {
		this.taskBiz = taskBiz;
	}

	public JsonModel getJm() {
		return jm;
	}

}
